<?php

namespace App\Http\Controllers\TeacherApi\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Users;
use App\Model\UserSign;
use Auth;
use App\Tools\UploadFiles;
use App\Model\UserSubject;

class UserController extends Controller
{
    // 修改用户信息
	public function edit_user_info(Request $request,Users $users){
		
		$data[$request->name] = $request->value;
		$userId = $request->id;
		$rs = $users->where('id',$userId)->update($data);
		if($rs){
			$this->data['msg'] = '修改成功！';
			$this->data['code'] = 1;
			$this->data['data'] = $data;
		}
		return $this->data;
	}

	// 根据id获取用户信息
	public function get_user(Request $request,Users $users){
		$userInfo = $users->find($request->id);
		$this->data['data'] = $userInfo;
		$this->data['code'] = 1;
		return $this->data;
	}

	// 修改头像
	public function avatar_upload(Request $request,Users $users){
		$uploads = new UploadFiles;//实例化上传类
		$id = $request->id;
		$is_upload = $uploads->uploads('file','180','180',1);
		if(!empty($is_upload)){
			$arr['avatar'] = $is_upload['smallPath'];
			$rs = $users->where('id',$id)->update($arr);
			if($rs){
				$this->data['msg'] = '上传成功！';
				$this->data['code'] = 1;
				$this->data['data'] = $arr;
			}
		}else{
			$this->data['msg'] = '上传失败！';
		}
		
		return $this->data;
	}


	// 获取打卡信息
	public function get_sign(Request $request,UserSign $userSign){
		$id = $request->id;
		$count = $userSign->where(['user_id'=>$id,'is_type'=>1])->count(); // 完成的打卡
		$todayCount = $userSign->where(['user_id'=>$id,'is_type'=>1,'sign_time'=>date('Ymd',time())])->count(); // 今天是否已经打卡
		$this->data['data']['count'] = $count;
		$this->data['data']['todayCount'] = $todayCount;
		$this->data['data']['time'] = date('Ymd',time());
		$this->data['code'] = 1;
		return $this->data;
	}

	// 增加打卡信息
	public function add_sign(Request $request,UserSign $userSign){
		$id = $request->id;
		$today = date('Ymd',time());
		$data = [
			'user_id'=>$id,
			'make_questions'=>'',
			'error_questions'=>'',
			'sign_time'=>$today,
			'add_time'=>time(),
		];
		$rs = $userSign->where(['user_id'=>$id,'sign_time'=>$today])->first();
		if(empty($rs)){
			$userSign->insert($data);
			$this->data['code'] = 1;
			$this->data['msg'] = '添加成功';
		}

		return $this->data;
		
	}

	// 保存今日做过的题目 错题
	public function save_questions(Request $request,UserSign $userSign,UserSubject $userSubject){
		$questions = $request->questions;
		$errors = $request->errors;
		$fraction = $request->fraction;
		$id = $request->id;
		$time = date('Ymd',time());
		$grade = $request->grade;
		$subject = $request->subject;
		$is_make = $request->is_make;
		$error_num = $request->error_num;
		$qid = $questions;
		$eid = $errors;

		$updateInfo = [
			'make_questions'=>$qid,
			'error_questions'=>$eid,
			'is_type'=>1,
			'complete_time'=>time(),
			'fraction'=>$fraction
		];

		if(empty($error_num)){
			$updateInfo['all_right_time'] = time();
		}

		if(empty($is_make)){
			$rs = $userSign->where(['user_id'=>$id,'sign_time'=>$time])->increment('make_num',1,$updateInfo);
		}else{
			$rs = $userSign->where(['user_id'=>$id,'sign_time'=>$time])->increment('make_num');
		}
		
		$whereInfo = [
			'user_id'=>$id,
			'grade_id'=>$grade,
			'subject_id'=>$subject
		];
		$info = $userSubject->where($whereInfo)->first();

		if(empty($is_make)){
			if(empty($info['questions'])){
				$questionData = ['questions'=>$qid];
				if(empty($info['errors'])){
					$questionData['errors'] = $eid;
				}
				$userSubject->where($whereInfo)->update($questionData);
			}else{
				$questionData = ['questions'=>$info['questions'].','.$qid];
				if(empty($info['errors'])){
					$questionData['errors'] = $info['errors'].','.$eid;
				}
				$userSubject->where($whereInfo)->update($questionData);
			}
		}

		$this->data['code'] = 1;
		$this->data['msg'] = '设置成功';

		return $this->data;
	}

}
